package com.jfcloud.gateway;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.gateway.config.LoadBalancerProperties;
import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Scope;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import com.alibaba.cloud.nacos.ribbon.NacosRule;
import com.netflix.loadbalancer.IRule;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;

/**
 * Nacos 启动器
 * 
 * @author zhaojunfu
 *
 */
@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan
@EnableAutoConfiguration
public class GateWayNacosApplication {

	public static void main(String[] args) {
		SpringApplication.run(GateWayNacosApplication.class, args);
	}

	@Bean
	@Scope(value="prototype")
	public IRule loadBalanceRule() {
		return new NacosRule();
	}

//	@Bean
//    public LoadBalancerClientFilter loadBalancerClientFilter(LoadBalancerClient client, LoadBalancerProperties properties) {
//        return new GatewayLoadBalancerClientFilter(client, properties);
//    }
	
	
	@Bean
    public RouterFunction<ServerResponse> indexRouter(
        @Value("classpath:/templates/swagger.html") final Resource indexHtml) {
        return route(GET("/swagger"), request -> ok().contentType(MediaType.TEXT_HTML).syncBody(indexHtml));
    }
}
